using System;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;
using Voodoo.Sauce.Internal.Analytics;
using Voodoo.Sauce.Internal.GDPR.UI;
using Voodoo.Sauce.Internal.GDPR.Utils;

namespace Voodoo.Sauce.Internal.GDPR
{
	public static class VoodooGDPR
	{
		public class GdprConsent
		{
			public bool IsGdprApplicable;

			public bool ExplicitConsentGivenForAds;

			public bool ExplicitConsentGivenForAnalytics;

			public bool IsEmbargoedCountry;

			public override string ToString()
			{
				return $"Applicable: {IsGdprApplicable}, Ads: {ExplicitConsentGivenForAds}, Analytics: {ExplicitConsentGivenForAnalytics}, Embargoed: {IsEmbargoedCountry}";
			}
		}

		private const string TAG = "VoodooGDPR";

		private const string PlayerPrefs_GdprAdsConsent = "Voodoo.GDPRConsent.Ads";

		private const string PlayerPrefs_GdprAnalyticsConsent = "Voodoo.GDPRConsent.Analytics";

		private const string PlayerPrefs_GdprApplicable = "Voodoo.GDPRApplicable";

		private const string PlayerPrefs_GdprTexts = "Voodoo.GDPRTexts";

		private static bool _enabled;

		private static bool _adsConsent;

		private static bool _analyticsConsent;

		private static string _idfa;

		private static bool _isGdprApplicable;

		private static bool _isInitialized;

		private static bool _limitAdTracking;

		private static Action<GdprConsent> _onConsentGiven;

		private static Action<GdprConsent> _onConsentUpdated;

		private static Action<bool> _gdprApplicableCallback;

		private static VoodooSettings _settings;

		private static Action _onGdprSettingsClosed;

		[CompilerGenerated]
		private static Action _003C_003Ef__mg_0024cache0;

		[CompilerGenerated]
		private static Action _003C_003Ef__mg_0024cache1;

		public static void Init(VoodooSettings settings)
		{
			_settings = settings;
			_enabled = true;
			VoodooSauceBehaviour.OnInternetConnect = (Action)Delegate.Combine(VoodooSauceBehaviour.OnInternetConnect, new Action(TryToShow));
			VoodooAnalytics.OnGamePlayed = (Action<int, bool>)Delegate.Combine(VoodooAnalytics.OnGamePlayed, (Action<int, bool>)delegate
			{
				if (!HasAlreadyAnswered())
				{
					InitWithIDFA();
				}
			});
			InitWithIDFA();
		}

		private static void InitWithIDFA()
		{
			if (_enabled)
			{
				VoodooLog.Log("VoodooGDPR", "InitWithIDFA");
				if (Debug.isDebugBuild)
				{
					VoodooSauceBehaviour.InvokeAfter(RequestIdfa, 1f);
				}
				else
				{
					RequestIdfa();
				}
			}
		}

		public static bool HasLimitAdTrackingEnabled()
		{
			return _limitAdTracking;
		}

		private static void RequestIdfa()
		{
			VoodooLog.Log("VoodooGDPR", "Requesting IDFA...");
			RequestIdfaAsyncWrapper(delegate(string id, bool enabled, string msg)
			{
				_limitAdTracking = !enabled;
				if (_limitAdTracking)
				{
					id = "00000000-0000-0000-0000-000000000000";
				}
				VoodooLog.Log("VoodooGDPR", $"IDFA get: {id}, enabled: {enabled}, error: {msg}");
				AskForConsent(id);
			});
		}

		public static string RandomString(int length)
		{
			return new string((from s in Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", length)
				select s[UnityEngine.Random.Range(0, s.Length)]).ToArray());
		}

		private static void RequestIdfaAsyncWrapper(Application.AdvertisingIdentifierCallback callback)
		{
			Application.RequestAdvertisingIdentifierAsync(callback);
		}

		private static void TryToShow()
		{
			if (_enabled && !VoodooAnalytics.HasGameStarted && !VoodooGDPRCanvas.IsShowing)
			{
				InitWithIDFA();
			}
		}

		private static void AskForConsent(string idfa)
		{
			_idfa = idfa;
			NeedConsentParams needConsentParams = new NeedConsentParams();
			needConsentParams.bundle_id = Application.identifier;
			needConsentParams.popup_version = VoodooGDPRUtils.GetPopupVersion();
			needConsentParams.user = VoodooGDPRUtils.GetUserStatus();
			needConsentParams.os_type = VoodooGDPRUtils.GetOsType();
			needConsentParams.app_version = VoodooGDPRUtils.GetAppVersion();
			needConsentParams.vs_version = VoodooGDPRUtils.GetVSVersion();
			needConsentParams.locale = VoodooGDPRUtils.GetLocale();
			needConsentParams.uuid = _idfa;
			needConsentParams.studio_name = "Voodoo";
			needConsentParams.gdpr_type = (int)VoodooGDPRUtils.GetGDPRType();
			VoodooGDPRAPI.NeedConsent(needConsentParams, delegate(NeedConsent consent)
			{
				_isGdprApplicable = consent.need_consent;
				if (_isGdprApplicable)
				{
					PlayerPrefs.SetInt("Voodoo.GDPRApplicable", 1);
				}
				else
				{
					PlayerPrefs.DeleteKey("Voodoo.GDPRApplicable");
				}
				VoodooLog.Log("VoodooGDPR", "IsGDPR Applicable: " + _isGdprApplicable);
				PlayerPrefs.SetString("Voodoo.GDPRTexts", consent.texts);
				if (_isGdprApplicable && !consent.embargoed_country)
				{
					VoodooGDPRCanvas.UpdateConsent(consent.ads_consent, consent.analytics_consent);
					VoodooGDPRCanvas.UpdateTexts(consent.texts);
					VoodooGDPRCanvas.Initialize();
					if (!consent.already_consent)
					{
						VoodooLog.Log("VoodooGDPR", "User never gave consent, showing optin");
						ShowOptIn();
					}
					else
					{
						VoodooLog.Log("VoodooGDPR", "User already consent, setting consent");
						SetConsent(consent.ads_consent, consent.analytics_consent);
						if (!consent.ads_consent || !consent.analytics_consent)
						{
							VoodooGDPRCanvas.ShowBanner();
						}
					}
				}
				else
				{
					VoodooLog.Log("VoodooGDPR", "Need consent is false (or embargoed country)");
					if (_onConsentGiven != null)
					{
						_onConsentGiven(new GdprConsent
						{
							IsGdprApplicable = false,
							ExplicitConsentGivenForAds = false,
							ExplicitConsentGivenForAnalytics = false,
							IsEmbargoedCountry = consent.embargoed_country
						});
					}
					_onConsentGiven = null;
					_enabled = false;
				}
				if (_gdprApplicableCallback != null)
				{
					_gdprApplicableCallback(_isGdprApplicable);
				}
				_gdprApplicableCallback = null;
				_isInitialized = true;
			}, delegate
			{
				VoodooLog.Log("VoodooGDPR", "Couldn't get Need Consent from server (No Internet?)");
				if (PlayerPrefs.HasKey("Voodoo.GDPRApplicable"))
				{
					bool adsConsent = HasAdsConsent();
					bool analyticsConsent = HasAnalyticsConsent();
					string @string = PlayerPrefs.GetString("Voodoo.GDPRTexts", "{}");
					VoodooGDPRCanvas.UpdateConsent(adsConsent, analyticsConsent);
					VoodooGDPRCanvas.UpdateTexts(@string);
					VoodooGDPRCanvas.Initialize();
					if (HasAlreadyAnswered())
					{
						_isGdprApplicable = PlayerPrefs.HasKey("Voodoo.GDPRApplicable");
						if (Application.internetReachability != 0)
						{
							SetConsent(adsConsent, analyticsConsent);
						}
					}
					else
					{
						ShowOptIn();
					}
				}
				else
				{
					if (_onConsentGiven != null)
					{
						_onConsentGiven(new GdprConsent
						{
							IsGdprApplicable = false,
							ExplicitConsentGivenForAds = false,
							ExplicitConsentGivenForAnalytics = false,
							IsEmbargoedCountry = false
						});
					}
					_onConsentGiven = null;
					_enabled = false;
				}
				if (_gdprApplicableCallback != null)
				{
					_gdprApplicableCallback(_isGdprApplicable);
				}
				_gdprApplicableCallback = null;
				_isInitialized = true;
			});
		}

		internal static void ShowOptIn()
		{
			if (_enabled)
			{
				VoodooGDPRCanvas.ShowOptIn();
			}
		}

		internal static void ShowOptInSettings(Action onSettingsClosed)
		{
			if (_enabled)
			{
				if (onSettingsClosed != null)
				{
					_onGdprSettingsClosed = onSettingsClosed;
				}
				VoodooGDPRCanvas.ShowSettings();
			}
		}

		public static void ShowGDPRBanner(bool force)
		{
			if (_enabled && (force || !_adsConsent || !_analyticsConsent))
			{
				VoodooGDPRCanvas.ShowBanner();
			}
		}

		public static void GiveConsent(bool adsConsent, bool analyticsConsent)
		{
			if (_enabled)
			{
				SetConsent(adsConsent, analyticsConsent);
				ConsentInsights(adsConsent, analyticsConsent);
			}
		}

		public static void CloseSettings()
		{
			if (_onGdprSettingsClosed != null)
			{
				_onGdprSettingsClosed();
			}
			_onGdprSettingsClosed = null;
		}

		private static void ConsentInsights(bool adsConsent, bool analyticsConsent)
		{
			if (_idfa == null)
			{
				Application.RequestAdvertisingIdentifierAsync(delegate(string id, bool enabled, string msg)
				{
					_idfa = id;
					SendConsentInsights(adsConsent, analyticsConsent);
				});
			}
			else
			{
				SendConsentInsights(adsConsent, analyticsConsent);
			}
		}

		private static void SendConsentInsights(bool adsConsent, bool analyticsConsent)
		{
			ConsentInsightsParams consentInsightsParams = new ConsentInsightsParams();
			consentInsightsParams.bundle_id = Application.identifier;
			consentInsightsParams.popup_version = VoodooGDPRUtils.GetPopupVersion();
			consentInsightsParams.user = VoodooGDPRUtils.GetUserStatus();
			consentInsightsParams.ads_consent = adsConsent;
			consentInsightsParams.analytics_consent = analyticsConsent;
			consentInsightsParams.os_type = VoodooGDPRUtils.GetOsType();
			consentInsightsParams.app_version = VoodooGDPRUtils.GetAppVersion();
			consentInsightsParams.vs_version = VoodooGDPRUtils.GetVSVersion();
			consentInsightsParams.locale = VoodooGDPRUtils.GetLocale();
			consentInsightsParams.uuid = _idfa;
			consentInsightsParams.studio_name = "Voodoo";
			consentInsightsParams.gdpr_type = (int)VoodooGDPRUtils.GetGDPRType();
			VoodooGDPRAPI.ConsentInsights(consentInsightsParams);
		}

		private static void SetConsent(bool adsConsent, bool analyticsConsent)
		{
			if (_enabled)
			{
				VoodooLog.Log("VoodooGDPR", $"User explicit consent: Ads: {adsConsent}, Analytics: {analyticsConsent}");
				_adsConsent = adsConsent;
				_analyticsConsent = analyticsConsent;
				PlayerPrefs.SetInt("Voodoo.GDPRConsent.Ads", _adsConsent ? 1 : 0);
				PlayerPrefs.SetInt("Voodoo.GDPRConsent.Analytics", _analyticsConsent ? 1 : 0);
				GdprConsent gdprConsent = new GdprConsent();
				gdprConsent.IsEmbargoedCountry = false;
				gdprConsent.IsGdprApplicable = true;
				gdprConsent.ExplicitConsentGivenForAds = _adsConsent;
				gdprConsent.ExplicitConsentGivenForAnalytics = _analyticsConsent;
				GdprConsent obj = gdprConsent;
				if (_onConsentGiven != null)
				{
					_onConsentGiven(obj);
				}
				else if (_onConsentUpdated != null)
				{
					_onConsentUpdated(obj);
				}
				_onConsentGiven = null;
			}
		}

		private static bool HasAlreadyAnswered()
		{
			return PlayerPrefs.HasKey("Voodoo.GDPRApplicable");
		}

		public static bool HasAdsConsent()
		{
			return PlayerPrefs.GetInt("Voodoo.GDPRConsent.Ads", 0) == 1;
		}

		public static bool HasAnalyticsConsent()
		{
			return PlayerPrefs.GetInt("Voodoo.GDPRConsent.Analytics", 0) == 1;
		}

		public static void SetCallbacks(Action<GdprConsent> onConsentGiven, Action<GdprConsent> onConsentUpdated)
		{
			_onConsentGiven = onConsentGiven;
			_onConsentUpdated = onConsentUpdated;
		}

		public static void RequestGdprApplicability(Action<bool> callback)
		{
			if (_isInitialized)
			{
				callback(_isGdprApplicable);
			}
			else
			{
				_gdprApplicableCallback = callback;
			}
		}

		public static bool IsGdprApplicable()
		{
			return _isGdprApplicable;
		}

		public static string GetIDFA()
		{
			return _idfa;
		}
	}
}
